package com.yoda.mall.mysql.api;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.FlashPromotionProductRelationApi;
import com.yoda.mall.model.entity.sms.Coupon;
import com.yoda.mall.model.entity.sms.FlashPromotionProductRelation;
import com.yoda.mall.mysql.mapper.FlashPromotionProductRelationMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class FlashPromotionProductRelationApiImpl implements FlashPromotionProductRelationApi {
    @Autowired
    private FlashPromotionProductRelationMapper flashPromotionProductRelationMapper;
    @Override
    public void addFlashPromotionProductRelation(FlashPromotionProductRelation flashPromotionProductRelation) {
        flashPromotionProductRelationMapper.insert(flashPromotionProductRelation);
    }

    @Override
    public void deleteFlashProductRelationById(Long id) {
        flashPromotionProductRelationMapper.deleteById(id);
    }

    @Override
    public Page<FlashPromotionProductRelation> getFlashProductRelationListPage(Long flashPromotionId, Long flashPromotionSessionId, Integer pageNum, Integer pageSize) {
        Page<FlashPromotionProductRelation> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<FlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(flashPromotionId), FlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        lqw.eq(ObjectUtil.isNotNull(flashPromotionSessionId), FlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);
        flashPromotionProductRelationMapper.selectPage(page, lqw);
        return page;
    }

    @Override
    public void updateFlashPromotionProductRelation(FlashPromotionProductRelation flashPromotionProductRelation) {
        flashPromotionProductRelationMapper.updateById(flashPromotionProductRelation);
    }

    @Override
    public FlashPromotionProductRelation getFlashPromotionProductRelationById(Long id) {
        return flashPromotionProductRelationMapper.selectById(id);
    }

    @Override
    public List<Long> getFlashPromotionSessionsIdByFlashPromotionId(Long flashPromotionId) {
        LambdaQueryWrapper<FlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtil.isNotNull(flashPromotionId), FlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        List<FlashPromotionProductRelation> flashPromotionProductRelations = flashPromotionProductRelationMapper.selectList(lqw);
        return CollUtil.getFieldValues(flashPromotionProductRelations,"flashPromotionSessionId",Long.class);
    }

    @Override
    public Long getCountByFlashPromotionIdAndFlashPromotionSessionId(Long flashPromotionId, Long flashPromotionSessionId) {
        LambdaQueryWrapper<FlashPromotionProductRelation> lqw = new LambdaQueryWrapper<>();
        lqw.eq(FlashPromotionProductRelation::getFlashPromotionId, flashPromotionId);
        lqw.eq( FlashPromotionProductRelation::getFlashPromotionSessionId, flashPromotionSessionId);
        Integer count = flashPromotionProductRelationMapper.selectCount(lqw);
        return count.longValue();
    }
}
